import 'package:flutter/material.dart';

///渐变颜色
class GradientColorWidget extends StatelessWidget {
  final Widget? child;
  final Widget? backgroundImage;
  final Color? formColor;
  final Color? toColor;
  final AlignmentGeometry? fromPosition;
  final AlignmentGeometry? toPosition;
  final List<Color>? colors; //当多种颜色的时候使用
  final BorderRadiusGeometry? borderRadius;
  final List<double>? stops;
  final double? height;
  final double? width;

  GradientColorWidget({
    Key? key,
    this.formColor,
    this.toColor,
    this.fromPosition,
    this.toPosition,
    this.colors,
    this.child,
    this.backgroundImage,
    this.borderRadius,
    this.stops,
    this.height,
    this.width,
  }) : super(key: key);

  Widget _content() {
    return Container(
      height: height,
      width: width,
      decoration: BoxDecoration(
        borderRadius: borderRadius,
        gradient: LinearGradient(
          begin: fromPosition ?? Alignment.topCenter,
          end: toPosition ?? Alignment.bottomCenter,
          stops: stops,
          colors: colors ?? [formColor!, toColor!],
        ),
      ),
      child: child,
    );
  }

  @override
  Widget build(BuildContext context) {
    if (backgroundImage != null) {
      return Stack(
        fit: StackFit.expand,
        children: [
          Positioned.fill(
            child: backgroundImage!,
          ),
          Positioned.fill(
            child: _content(),
          ),
        ],
      );
    } else {
      return _content();
    }
  }
}
